package org.example.service.impl;


import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.ResponseResult;
import org.example.mapper.UploadMapper;
import org.example.service.UploadService;
import org.example.utils.PathUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.IOException;
import java.io.InputStream;

@Service
@Data
@Slf4j
@ConfigurationProperties(prefix = "oss")
@RequiredArgsConstructor
public class UploadServiceImpl implements UploadService {

    private String accessKey;
    private String secretKey;
    private String bucketName;
    private String endpoint;
    private final UploadMapper uploadMapper;
    @Override
    public ResponseResult uploadImg(MultipartFile img) {

        String originalFilename = img.getOriginalFilename();

        String filePath = PathUtils.generateFilePath(originalFilename);

        String url = uploadOss(img, filePath);

        return ResponseResult.okResult(url);
    }

    private String uploadOss(MultipartFile imgFile, String filePath){

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKey,secretKey);

        InputStream inputStream = null;

        try {
            inputStream = imgFile.getInputStream();;
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, filePath, inputStream);
            PutObjectResult result = ossClient.putObject(putObjectRequest);

            String url="https://"+bucketName+"."+endpoint.split("//")[1]+"/"+filePath;
            return url;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            log.error("文件上传失败=======>"+e.getMessage());
            throw new RuntimeException(e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return "上传失败";
    }
}
